home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / lpDaemon SRC / lpd Sources / Main.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-31  |  8.3 KB  |  347 lines  |  [TEXT/KAHL]

  1. /************************************************************************
  2.  *                                                                        *
  3.  *    Main.c                                                                *
  4.  *                                                                        *
  5.  *        -------------- The event handling routines --------------        *
  6.  *                                                                        *
  7.  *  Written by Casper Boon, August, 1992.                                *
  8.  *                                                                        *
  9.  *    © 1992 Casper Boon.                                                    *
  10.  *                                                                        *
  11.  ************************************************************************/
  12.  
  13. #include "LPD.H"
  14. #include "BackGrounder.H"
  15. #include "lpdProtos.H"
  16.  
  17.  
  18. void DoWindowActivate(WindowPtr wind, Boolean actv);
  19. void DoWindowUpdate(WindowPtr wind);
  20. void DoWindowClick(WindowPtr wind, Point point, integer modif);
  21. void DoWindowGrow(WindowPtr wind, Point point);
  22. void DoWindowZoom(WindowPtr wind, Boolean in);
  23. void DoWindowKey(WindowPtr wind, char c);
  24. LongWord GetSleep(void);
  25. void DoIdles(void);
  26.  
  27. /************************************************************************
  28.  *                                                                        *
  29.  *                                                                        *
  30.  ************************************************************************/
  31. void main()
  32. {
  33.     Initialise();
  34.     UnloadSeg(Initialise);
  35.  
  36.     SetCursor(&arrow);
  37.  
  38.     if (!InitLPD())
  39.         {
  40.         SysBeep(5);
  41.         Exit();
  42.         }
  43.  
  44.     while (TRUE)
  45.         MainEvents(everyEvent);
  46. }
  47.  
  48.  
  49. /************************************************************************
  50.  *                                                                        *
  51.  *                                                                        *
  52.  ************************************************************************/
  53. void WaitForEvent(integer evtMask, EventPtr event)
  54. {
  55.     Boolean    gotEvent;
  56.  
  57.     do    {
  58.         if (haveWaitEvnt)
  59.             gotEvent = WaitNextEvent(evtMask, event, GetSleep(), cursRgn);
  60.         else
  61.             {
  62.             SystemTask();                                 /* run drivers    */
  63.             gotEvent = GetNextEvent(evtMask, event);
  64.             }
  65.  
  66.         CursorAdjust(event->where, cursRgn);            /* update cursor */
  67.  
  68.         if (!gotEvent)
  69.             {
  70.             if (event->what == nullEvent)
  71.                 {
  72.                 IsDialogEvent(event);           /* flick TE's in dialogs    */
  73.                 DoIdles();                      /* run the idle procedures */
  74.                 }
  75.             }
  76.         }
  77.     while (!gotEvent);
  78. }
  79.  
  80. /************************************************************************
  81.  *                                                                        *
  82.  *                                                                        *
  83.  ************************************************************************/
  84. integer MainEvents(integer evtMask)
  85. {
  86.     integer        part;
  87.     WindowPtr    window;
  88.     EventRecord    event;
  89.  
  90.     if (QuitRequest) Exit();
  91.  
  92.     WaitForEvent(evtMask, &event);
  93.  
  94.     if ( (event.what == keyDown) && (event.modifiers & cmdKey) )
  95.         {
  96.         if ( (event.message & 0x7F) == '.' )
  97.             stopped = TRUE;
  98.         else
  99.             {
  100.             SetMenuStates();
  101.             DoCommand(MenuKey(event.message & 0x7F));
  102.             }
  103.         return TRUE;
  104.         }
  105.  
  106.     if ( IsDialogEvent(&event) )
  107.         {
  108.         if ( HandleDEvent(&event) )
  109.             return FALSE;
  110.         }
  111.  
  112.     switch (event.what)
  113.         {
  114.         case mouseDown :
  115.             part = FindWindow(event.where, &window);
  116.             switch(part)
  117.                 {
  118.                 case inMenuBar :
  119.                     SetMenuStates();
  120.                     DoCommand(MenuSelect(event.where));
  121.                     break;
  122.                 case inSysWindow :
  123.                     SystemClick(&event, window);
  124.                     break;
  125.                 case inDrag :
  126.                     if (window == FrontWindow())
  127.                         DragWindow(window, event.where, &screenRect);
  128.                     else
  129.                         SelectWindow(window);
  130.                     break;
  131.                 case inGrow :
  132.                     if (window == FrontWindow())
  133.                         DoWindowGrow(window, event.where);
  134.                     else
  135.                         SelectWindow(window);
  136.                     break;
  137.                 case inGoAway :
  138.                     if ( TrackGoAway(window, event.where) )
  139.                         KillWindow((WindowPeek)window);
  140.                     break;
  141.                 case inContent :
  142.                     if ( window == FrontWindow() )
  143.                         DoWindowClick(window, event.where, event.modifiers);
  144.                     else
  145.                         SelectWindow(window);
  146.                     break;
  147.                 case inZoomIn :
  148.                 case inZoomOut :
  149.                     if ( TrackBox(window, event.where, part) )
  150.                         DoWindowZoom(window, (Boolean)(part == inZoomIn));
  151.                     break;
  152.                 }
  153.             break;
  154.         case keyDown:
  155.         case autoKey:
  156.             DoWindowKey(FrontWindow(), event.message & 0x7F);
  157.             break;
  158.         case activateEvt :
  159.             DoWindowActivate((WindowPtr) event.message, event.modifiers & activeFlag);
  160.             break;
  161.         case updateEvt :
  162.             window = (WindowPtr) event.message;
  163.             BeginUpdate(window);
  164.             SetPort(window);
  165.             DoWindowUpdate(window);
  166.             EndUpdate(window);
  167.             break;
  168.         case osEvt:
  169.             switch ((event.message >> 24) & 0xFF)    /* high byte of message */
  170.                 {
  171.                 case mouseMovedMessage:
  172.                     DoIdles();
  173.                     break;
  174.                 case suspendResumeMessage:
  175.                     inBackground = ((event.message & resumeFlag) == 0);
  176.                     if (inBackground) Banners_Down();
  177.                     else              Banners_Back();
  178.                     DoWindowActivate(FrontWindow(), !inBackground);
  179.                     break;
  180.                 default:
  181.                     break;
  182.                 }
  183.             break;
  184.         default :
  185.             break;
  186.         }
  187.  
  188.     return FALSE;
  189. }
  190.  
  191.  
  192. /************************************************************************
  193.  *                                                                        *
  194.  *                                                                        *
  195.  ************************************************************************/
  196. void BeepNWait()
  197. {
  198.     SysBeep(5);
  199.     while ( !Button() );
  200.     while (  Button() );
  201. }
  202.  
  203.  
  204. /************************************************************************
  205.  *                                                                        *
  206.  *                                                                        *
  207.  ************************************************************************/
  208. integer KillWindow(WindowPeek wind)
  209. {
  210.     return TRUE;
  211. }
  212.  
  213. /************************************************************************
  214.  *                                                                        *
  215.  *                                                                        *
  216.  ************************************************************************/
  217. static void DoWindowActivate(WindowPtr wind, Boolean activ)
  218. {
  219. }
  220.  
  221.  
  222. /************************************************************************
  223.  *                                                                        *
  224.  *                                                                        *
  225.  ************************************************************************/
  226. static void DoWindowUpdate(WindowPtr wind)
  227. {
  228. }
  229.  
  230.  
  231. /************************************************************************
  232.  *                                                                        *
  233.  *                                                                        *
  234.  ************************************************************************/
  235. static void DoWindowClick(WindowPtr wind, Point point, integer modif)
  236. {
  237. }
  238.  
  239.  
  240. /************************************************************************
  241.  *                                                                        *
  242.  *                                                                        *
  243.  ************************************************************************/
  244. static void DoWindowGrow(WindowPtr wind, Point point)
  245. {
  246. }
  247.  
  248.  
  249. /************************************************************************
  250.  ************************************************************************/
  251. static void DoWindowKey(WindowPtr wind, char aChar)
  252. {
  253. }
  254.  
  255.  
  256. /************************************************************************
  257.  ************************************************************************/
  258. static void DoWindowZoom(WindowPtr aWind, Boolean in)
  259. {
  260. }
  261.  
  262. void doClose(void);
  263.  
  264. /************************************************************************
  265.  *                                                                        *
  266.  *                                                                        *
  267.  ************************************************************************/
  268. void Exit()
  269. {
  270.     QuitRequest = TRUE;
  271.     if (!Quitable) return;
  272.  
  273.     doClose();
  274.     FSClose(stdlog);
  275.     FSClose(stderr);
  276.     ExitToShell();
  277. }
  278.  
  279. /************************************************************************
  280.  ************************************************************************/
  281. void CursorAdjust(Point aPoint, RgnHandle crsRgn)
  282. {
  283. }
  284.  
  285. /************************************************************************
  286.  *                                                                        *
  287.  *                                                                        *
  288.  ************************************************************************/
  289. void UpdateEvents()
  290. {
  291. }
  292.  
  293.  
  294. /************************************************************************
  295.  *                                                                        *
  296.  *                                                                        *
  297.  ************************************************************************/
  298. void DoIdles()
  299. {
  300.     RunBackground();
  301. }
  302.  
  303. /************************************************************************
  304.  *                                                                        *
  305.  *                                                                        *
  306.  ************************************************************************/
  307. LongWord GetSleep()
  308. {
  309.     LongInt    sleep = GetCaretTime();
  310.  
  311. //    if ( !inBackground )
  312. //        sleep = 0x7F;
  313.  
  314.     return sleep;
  315. }
  316.  
  317. /************************************************************************
  318.  *                                                                        *
  319.  *                                                                        *
  320.  ************************************************************************/
  321. Boolean noMenus = FALSE;
  322. void WaitForState(integer *state)
  323. {
  324.     Boolean quit_back = Quitable;
  325.     EventRecord    event;
  326.  
  327.     Quitable = FALSE;
  328.     noMenus = TRUE;
  329.  
  330.     do    {
  331.         if ( !EventAvail(everyEvent, &event) )
  332.             {
  333.             if (haveWaitEvnt)
  334.                 WaitNextEvent(everyEvent, &event, GetSleep(), cursRgn);
  335.             else
  336.                 SystemTask();                                 /* run drivers    */
  337.             }
  338.         else
  339.             MainEvents(everyEvent);
  340.         RunBackground();
  341.         }
  342.     while (*state > 0);
  343.  
  344.     noMenus = FALSE;
  345.     Quitable = quit_back;
  346. }
  347.